package com.twotiger.and.activity.user;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.Constants;
import com.twotiger.and.activity.WebViewPage;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.bean.ConstantDataUnit;
import com.twotiger.and.util.DigestUtils;
import com.twotiger.and.util.PreferencesUtils;
import com.twotiger.and.util.PromptManager;
import com.twotiger.and.util.PromptManager.ReChargeClickListener;
import com.twotiger.and.util.StringUtils;
import com.twotiger.and.view.GesturePassWordView;

/**
 * 设置手势
 * @Description: TODO(这里描述这个类的作用) 
 * @Author douxy
 * @Date 2015年8月17日 下午11:56:22
 */
public class SetGesturePsdPage extends BaseActivity implements OnClickListener {

    private com.twotiger.and.view.GesturePassWordView mGesturePassWordView;
    private ImageView gesture_cache_img;
    private TextView tvReset;
    private TextView gesture_hint_tv;
    private int errorCount = 0;
    String pre_gesture_psd = null;
    private TextView welcome_text;

    private SubTitle subTitle;
    private Bitmap drawingCache;

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.set_gesture_password_layout, null);
        return view;
    }

    @Override
    protected void findViewById(View view) {
        mGesturePassWordView = (GesturePassWordView) view.findViewById(R.id.mGesturePassWordView);
        gesture_cache_img = (ImageView) view.findViewById(R.id.gesture_cache_img);
        tvReset = (TextView) view.findViewById(R.id.tvReset);
        gesture_hint_tv = (TextView) view.findViewById(R.id.gesture_hint_tv);
        welcome_text = (TextView) view.findViewById(R.id.welcome_text);
        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {

            }

            @Override
            protected void clickRightBtn(View v) {

                PromptManager.showConfirmAlertCommon(mContext, 0, "温馨提示",
                    "跳过设置手势，下次需要重新登录", "继续设置", "跳过", true, new ReChargeClickListener() {

                        @Override
                        public void onClickConfirm() {

                        }

                        @Override
                        public void onClickCancel() {
                            PreferencesUtils.putString(mContext, Constants.token, null);
                            if (getIntent().getBooleanExtra("formregist", false)) {
                                ConstantDataUnit dz_zccg = getConstantDataByKey(Constants.dz_zccg);
                                if (dz_zccg != null) {
                                    Intent intent = new Intent(mContext, WebViewPage.class);
                                    intent.putExtra("url", dz_zccg.getVal());
                                    intent.putExtra("title", dz_zccg.getName());
                                    startNewActivity(intent, R.anim.fade_in_anim,
                                        R.anim.slide_out_to_bottom, true);
                                }
                            } else {
                                onBackPress();
                            }
                            application.closeGesture();
                        }
                    });

            }

            @Override
            protected void cickLeftBtn(View v) {
            }
        };
        subTitle.setTitle("设置手势密码");
        subTitle.hideBtns();
        subTitle.hideBottomLine();
        subTitle.title_right_tv.setVisibility(View.VISIBLE);
        subTitle.title_right_tv.setText("跳过");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.tvReset:
            pre_gesture_psd = null;
            mGesturePassWordView.clearPassword();
            gesture_hint_tv.setText("请设置启动密码");
            gesture_hint_tv.setTextColor(SetGesturePsdPage.this.getResources().getColor(
                R.color.white));
            gesture_cache_img.setImageBitmap(null);
            break;

        }
    }

    @Override
    protected void setListener() {
        tvReset.setOnClickListener(this);
    }

    @Override
    protected void processLogic() {
        welcome_text.setText("" + StringUtils.hideMidString(application.getUser().phone, 3, 4));
        mGesturePassWordView.invalidate();
        mGesturePassWordView.refreshDrawableState();
        mGesturePassWordView.requestLayout();
        application.setGesturePwd(null);//清空手势密码
        mGesturePassWordView.setDrawingCacheEnabled(true);
        mGesturePassWordView.setOnCompleteListener(new GesturePassWordView.OnCompleteListener() {

            @Override
            public void onComplete(String mPassword) {

                mPassword = DigestUtils.md5(mPassword);
                if (StringUtils.isEmpty(pre_gesture_psd)) {
                    drawingCache = mGesturePassWordView.getDrawingCache();
                    gesture_cache_img.setImageBitmap(drawingCache);
                    pre_gesture_psd = mPassword;
                    gesture_hint_tv.setText("请再次绘制解锁密码");
                    gesture_hint_tv.setTextColor(SetGesturePsdPage.this.getResources()
                        .getColor(R.color.gesture_tx_color));
                    mGesturePassWordView.clearPassword();
                } else {
                    if (pre_gesture_psd.equals(mPassword)) {
                        application.setGesturePwd(mPassword);
                        mGesturePassWordView.resetPassWord(mPassword);
                        mGesturePassWordView.clearPassword();
                        if (getIntent().getBooleanExtra("formregist", false)) {
                            ConstantDataUnit dz_zccg = getConstantDataByKey(Constants.dz_zccg);
                            if (dz_zccg != null) {
                                Intent intent = new Intent(mContext, WebViewPage.class);
                                intent.putExtra("url", dz_zccg.getVal());
                                intent.putExtra("title", dz_zccg.getName());
                                startNewActivity(intent, R.anim.fade_in_anim,
                                    R.anim.slide_out_to_bottom, true);
                            }
                        } else {
                            onBackPress();
                        }
                    } else {
                        errorCount++;
                        if (errorCount > 5) {

                            final AlertDialog alertDialog = PromptManager.showSimpleCustomDialog(
                                SetGesturePsdPage.this, "提示", "您输入错误次数过多", "确认",
                                new OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        popBackStack();
                                    }
                                });
                            if (alertDialog != null) {
                                alertDialog.show();
                            }
                            errorCount = 0;
                        }
                        //                        pre_gesture_psd = null;  //设置手势密码逻辑是 记录第一次的手势，第二次错了，第三次和第一次一样即可设置成功
                        gesture_hint_tv.setText("与第一次绘制不一致，请重新绘制");
                        gesture_hint_tv.setTextColor(SetGesturePsdPage.this.getResources()
                            .getColor(R.color.red));
                        gesture_cache_img.setImageBitmap(drawingCache);
                        mGesturePassWordView.clearPassword();
                    }
                }
            }

            @Override
            public void onShortComplete(String password) {
                if (drawingCache != null) {
                    gesture_hint_tv.setText("与第一次绘制不一致，请重新绘制");
                }else{
                    gesture_hint_tv.setText("至少连接4个点");
                }
                
                //                pre_gesture_psd = null;
                gesture_hint_tv.setTextColor(SetGesturePsdPage.this.getResources().getColor(
                    R.color.red));
                if (drawingCache != null) {
                    gesture_cache_img.setImageBitmap(drawingCache);
                }
                mGesturePassWordView.clearPassword();
            }
        });
    }

    @Override
    protected void onBackPress() {
        finish();
        overridePendingTransition(R.anim.silent_anim, R.anim.slide_out_to_bottom);
    }

}
